﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatrixInSpiral
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] input = new int[5, 5];
            int count = 1;
            for (int i = 0; i < input.GetLength(0); i++)
            {
                for (int j = 0; j < input.GetLength(0); j++)
                {
                    input[i, j] = count++;
                }
            }

            Spiral(input);

            Console.Read();
        }

        public static void Spiral(int[,] input)
        {
            int length = input.GetLength(0) - 1;

            for (int i = 0; i < input.GetLength(0) / 2; i++)
            {
                int k = length - i; //current bound

                for (int j = i; j < k; j++)
                    Console.Write("{0} ", input[i, j]);

                for (int j = i; j < k; j++)
                    Console.Write("{0} ", input[j, k]);

                for (int j = k; j > i; j--)
                    Console.Write("{0} ", input[k, j]);

                for (int j = k; j > i; j--)
                    Console.Write("{0} ", input[j, i]);
            }

            if (input.GetLength(0) % 2 == 1)
                Console.Write(input[input.GetLength(0) / 2, input.GetLength(0) / 2]);
        }
    }
}
